﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using IMCOM.Imagiality.ImageProcessing;
using IMCOM.Imagiality.ImageProcessing.Canvas2D;
using IMCOM.Imagiality.Core;

namespace IMCOM.Imagiality.MainGUI
{
    public class Step2GUI : Panel, ScanImagesGUIListener
    {
        private const int RightPanelWidth = 200;

        private GenericImage image;
        private Canvas2DGUI canvas2DGUI;
        private Panel panelRight;
        private Button buttonCropImage;
        private ScanImagesGUI scanImagesGUI;
        private Button buttonBack;
        private Button buttonNext;
        private IList<Step2GUIListener> listeners;

        public GenericImage Image
        {
            get { return image; }
            set { image = value; setImage(value); }
        }

        public ScanImagesGUI ScanImagesGUI
        {
            get { return scanImagesGUI; }
        }

        public Step2GUI()
        {
            listeners = new List<Step2GUIListener>();

            Width = 640;
            Height = 490;

            canvas2DGUI = new Canvas2DGUI();
            Controls.Add(canvas2DGUI);

            panelRight = new Panel();
            panelRight.Parent = this;

            buttonCropImage = new Button();
            panelRight.Controls.Add(buttonCropImage);
            buttonCropImage.Text = "Crop Image";

            scanImagesGUI = new ScanImagesGUI();
            panelRight.Controls.Add(scanImagesGUI);
            scanImagesGUI.AddListener(this);

            buttonBack = new Button();
            Controls.Add(buttonBack);
            buttonBack.Text = "Back";

            buttonNext = new Button();
            Controls.Add(buttonNext);
            buttonNext.Text = "Next";

            Resize += new EventHandler(GUIResized);

            ArrangeLayout();
        }

        private void setImage(GenericImage image)
        {
            IList<GenericImage> images = new List<GenericImage>();
            images.Add(image);
        
            CanvasImageWrapper imageWrapper = new CanvasImageWrapper(images);
            canvas2DGUI.ImageWrapper = imageWrapper;
        }

        public void AddListener(Step2GUIListener listener)
        {
            listeners.Add(listener);
            scanImagesGUI.AddListener(listener);
        }

        private void GUIResized(object sender, System.EventArgs e)
        {
            ArrangeLayout();
        }

        private void ArrangeLayout()
        {
            canvas2DGUI.Location = new Point(5, 5);
            canvas2DGUI.Width = Width - 5 - RightPanelWidth - 5 - 5;
            canvas2DGUI.Height = Height - 5 - 5 - buttonNext.Height - 5;

            panelRight.Location = new Point(Width - RightPanelWidth - 5, 0);
            panelRight.Width = RightPanelWidth;
            panelRight.Height = Height - 5 -buttonNext.Height - 5;

            buttonCropImage.Location = new Point(0, 5);
            buttonCropImage.Width = panelRight.Width;

            scanImagesGUI.Location = new Point(0, buttonCropImage.Bottom + 5);
            scanImagesGUI.Width = RightPanelWidth;

            buttonBack.Left = 5;
            buttonBack.Top = Height - buttonBack.Height - 5;
            
            buttonNext.Left = Width - buttonNext.Width - 5;
            buttonNext.Top = Height - buttonNext.Height - 5;
        }

        public void scanLevelNumericUpDownValueChanged(object sender, EventArgs e)
        {
        }

        public void takeScanImagesButtonClicked(object sender, EventArgs e)
        {
        }

        public void scanImagesListBoxSelectedValueChanged(object sender, EventArgs e)
        {
        }

        public void canvasImageWrapperChanged()
        {
            canvas2DGUI.ImageWrapper = scanImagesGUI.CanvasImageWrapper;
        }

        public void cleanListButtonClicked(object sender, EventArgs e)
        {
        }
    }
}
